<template>
  <div class="login-container">
    <div class="login-box">
      <h2>login</h2>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item class="login-btn-box">
          <el-button type="primary" class="login-btn" @click="submitForm(ruleFormRef)" :loading="loading" round>
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
// FormRules - 表单验证规则类型
// FormInstance - 表单实例类型
import type { FormInstance, FormRules } from 'element-plus'

//加载状态
const loading = ref(false)
//表单内容参数类型
interface RuleForm {
  username: string,
  password: string
}
//表单内容
const ruleForm = reactive<RuleForm>({
  username: '',
  password: ''
})
//form表单实例
const ruleFormRef = ref<FormInstance>()


//表单验证规则
const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      setTimeout(function () {
        console.log('成功!')
        loading.value = false
        ElMessage.success('登录成功')
      }, 1000)
    } else {
      console.log('失败!', fields)
    }
  })
}



</script>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url(../assets/image/login/bg.jpeg) center / cover no-repeat;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

  h2 {
    text-align: center;
    margin-bottom: 40px;
  }
}

.title {
  text-align: center;
  color: #303133;
  margin-bottom: 30px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.login-btn-box {
  :deep() {
    .el-form-item__content {
      margin-left: 0 !important;
    }
  }

  .login-btn {
    width: 100%;
    margin-top: 10px;
  }
}
</style>
